package apis

import (
	"encoding/xml"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"net/url"
	"strconv"
	"time"
	"vrcm/src/vrcm/app/admin/models"
	"vrcm/src/vrcm/app/admin/models/live"
	"vrcm/src/vrcm/app/admin/service/dto/live"
	vrmDto "vrcm/src/vrcm/app/admin/service/dto/vrm"
	"vrcm/src/vrcm/app/admin/service/live"
	"vrcm/src/vrcm/common/apis"
	ext "vrcm/src/vrcm/envconfig"

	wcDto "vrcm/src/vrcm/app/admin/service/dto/wechat"
	wc "vrcm/src/vrcm/app/admin/service/wechat"
)

type LiveBasic struct {
	apis.Api
}

// GetPage 获取LiveBasic列表
// @Summary 获取LiveBasic列表
// @Description 获取LiveBasic列表
// @Tags LiveBasic
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.LiveBasic}} "{"code": 200, "data": [...]}"
// @Router /api/v1/live-basic [get]
// @Security Bearer
func (e LiveBasic) GetPage(c *gin.Context) {
	req := dto.LiveBasicGetPageReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}

	req.IsDelete = 2
	list := make([]live.LiveBasic, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取LiveBasic失败，失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

func (e LiveBasic) GetPageForEnterprise(c *gin.Context) {
	req := dto.LiveBasicGetPageReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	req.IsDelete = 2
	list := make([]live.LiveBasic, 0)
	var count int64

	err = s.GetPageForEnterprise(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取LiveBasic失败，失败信息 %s", err.Error()))
		return
	}

	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取LiveBasic
// @Summary 获取LiveBasic
// @Description 获取LiveBasic
// @Tags LiveBasic
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.LiveBasic} "{"code": 200, "data": [...]}"
// @Router /api/v1/live-basic/{id} [get]
// @Security Bearer
func (e LiveBasic) Get(c *gin.Context) {
	req := dto.LiveBasicGetReq{}
	s := service.LiveBasic{}
	//imService := im.IMService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	var object live.LiveBasicWithStream
	if len(c.Query("permissionTenantId")) > 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	err = s.Get(&req, &object)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取LiveBasic失败，失败信息 %s", err.Error()))
		return
	}
	//获取直播间信息
	//if object.BasicInfo.EnableIM == 1 {
	//	infos, err := imService.GetGroupInfo(imDto.GetGroupInfoReqDto{TenantId: object.BasicInfo.TenantId, GroupIds: []string{object.BasicInfo.GroupId}})
	//	if err != nil {
	//		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取GetGroupInfo失败，失败信息 %s", err.Error()))
	//		return
	//	}
	//
	//}

	if object.BasicInfo.Id > 0 {
		object.GroupInfos = make([]live.GroupInfo, 0, 10)
		g := live.GroupInfo{}
		g.Appid = object.BasicInfo.AppId
		g.GroupId = object.BasicInfo.GroupId
		object.GroupInfos = append(object.GroupInfos, g)
		object.CorpId = models.CorpId
		object.AgentId = models.AppId
		object.PublicAgentId = models.PublicAppId
		object.EnterpriseWxAppId = models.AgentId

		urlString := ext.ExtConfig.ServiceConfig.WebClientServiceUrl + "basic/live" + "?"
		params := url.Values{}
		id := strconv.Itoa(object.BasicInfo.Id)
		params.Set("id", id)
		params.Set("tenant_id", object.BasicInfo.TenantId)
		params.Set("type", "liveStream")
		urlString = urlString + url.QueryEscape(params.Encode())
		object.LiveAddress = urlString
	}
	e.OK(object, "查询成功")
}

// Insert 创建直播间
func (e LiveBasic) Insert(c *gin.Context) {
	req := dto.LiveBasicInsertReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	req.CreateIM.TenantId = c.Query("permissionTenantId")
	if len(c.Query("permissionTenantId")) > 0 {
		req.TenantId = c.Query("permissionTenantId")
	}

	now := time.Now().Unix()
	if req.LiveTime <= now {
		e.Error(apis.LiveTimeError.Code, err, fmt.Sprintf("创建LiveBasic失败，失败信息 %s", apis.LiveTimeError.Msg))
		return
	}
	isExisted := s.IsExitLiveBasic(&dto.LiveBasicReq{LiveName: req.LiveName, TenantId: req.TenantId})
	if isExisted {
		e.Custom("", apis.RepeatedDataError)
		return
	}

	//创建直播间
	err = s.Insert(&req)
	if err != nil {
		if err.Error() == string(apis.MaxPushNumError.Code) {
			e.Error(apis.MaxPushNumError.Code, err, "创建LiveBasic失败，失败信息: 已超过最大转推数")
		} else if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.MaxPushNumError.Code, err, "创建LiveBasic失败，失败信息: 图片地址不正确")
		} else {
			e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建LiveBasic失败，失败信息 %s", err.Error()))
		}
		return
	}

	e.OK(req.GetId(), "创建成功")
}

func (e LiveBasic) InsertVodLive(c *gin.Context) {
	req := dto.LiveBasicInsertReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	//创建直播间
	err = s.InsertVod(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建InsertVodLive失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

func (e LiveBasic) PublishVod(c *gin.Context) {
	req := dto.PublishVodLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	//创建直播间
	err = s.PublishVod(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("发布失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "发布成功")
}

// Update 修改LiveBasic
// @Summary 修改LiveBasic
// @Description 修改LiveBasic
// @Tags LiveBasic
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.LiveBasicUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/live-basic/{id} [put]
// @Security Bearer
func (e LiveBasic) Update(c *gin.Context) {
	req := dto.LiveBasicUpdateReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	isExisted := s.IsExitLiveBasic(&dto.LiveBasicReq{Id: req.Id, LiveName: req.LiveName, TenantId: req.TenantId})
	if isExisted {
		e.Custom("", apis.RepeatedDataError)
		return
	}

	err = s.Update(&req)
	if err != nil {
		if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.ImageError.Code, err, "修改LiveBasic失败: 图片地址不正确")
		} else {
			e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改LiveBasic失败，失败信息 %s", err.Error()))
		}

		return
	}

	e.OK(req.GetId(), "修改成功")
}

func (e LiveBasic) SaveCorpId(c *gin.Context) {
	req := dto.SaveCorpIdReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveCorpId(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改LiveBasic失败，失败信息 %s", err.Error()))
		return
	}
	e.OK(req.Id, "修改成功")
}

// Delete 删除LiveBasic
// @Summary 删除LiveBasic
// @Description 删除LiveBasic
// @Tags LiveBasic
// @Param data body dto.LiveBasicDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/live-basic [delete]
// @Security Bearer
func (e LiveBasic) Delete(c *gin.Context) {
	s := service.LiveBasic{}
	//imService := im.IMService{}
	req := dto.LiveBasicDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	model := live.LiveBasicWithStream{}
	s.Get(&dto.LiveBasicGetReq{Id: req.Id, TenantId: req.TenantId}, &model)
	if model.BasicInfo.LiveState == 2 || model.BasicInfo.LiveState == 3 {
		e.Error(apis.DeleteLiveError.Code, err, fmt.Sprintf("删除LiveBasic失败，失败信息 %s", apis.DeleteLiveError.Msg))
		return
	}
	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除LiveBasic失败，失败信息 %s", err.Error()))
		return
	}

	//关闭聊天室
	//imService.DestroyIM(imDto.IMDestroyReqDto{TenantId: model.BasicInfo.TenantId, GroupId: model.BasicInfo.GroupId})
	e.OK(req.GetId(), "删除成功")
}

// UpdateLiveStream 更新直播流信息
func (e LiveBasic) UpdateLiveStream(c *gin.Context) {
	req := dto.UpdateLiveStreamExpireTimeReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.UpdateLiveStreamError.Code, err, err.Error())
		return
	}
	convertTime, err := time.ParseInLocation("2006-01-02 15:04:05", req.Expiry, time.Local)
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.UpdateLiveStreamError.Code, err, err.Error())
		return
	}
	expireTime := convertTime.Unix()
	now := time.Now()
	later := now.AddDate(0, 0, 30).Unix()

	if expireTime <= now.Unix() {
		e.Error(apis.PushStreamTimeBeforeError.Code, err, fmt.Sprintf("推流有效时间设置失败，失败信息 %s", apis.PushStreamTimeBeforeError.Msg))
		return
	}

	if expireTime > later {
		e.Error(apis.PushStreamTimeError.Code, err, fmt.Sprintf("推流有效时间设置失，失败信息 %s", apis.PushStreamTimeError.Msg))
		return
	}
	req.TenantId = c.Query("permissionTenantId")
	//调用更新直播流
	req.ExpireTime = int64(convertTime.Sub(now).Seconds())
	err = s.UpdateLiveStreamExpireTime(&req)
	if err != nil {
		e.Error(apis.CreateLiveStreamError.Code, err, fmt.Sprintf("更新直播流过期时间，失败信息 %s", err.Error()))
		return
	}

	e.OK("", "更新直播流信息成功")
}

// DressLive 开启直播
func (e LiveBasic) DressLive(c *gin.Context) {
	req := dto.StartOrEndLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DressLive(&req)
	if err != nil {
		e.Error(apis.DressLiveError.Code, err, fmt.Sprintf("直播彩排失败，失败信息 %s", err.Error()))
		return
	}

	e.OK("", "直播彩排成功")
}

// StartLive 开启直播
func (e LiveBasic) StartLive(c *gin.Context) {
	req := dto.StartOrEndLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StartLive(&req)
	if err != nil {
		if err.Error() == "170002" {
			e.Error(apis.PushTimeExpireError.Code, err, fmt.Sprintf("直播间推流地址过期，请重置推流地址"))
		} else if err.Error() == string(apis.ResetPullStreamError.Code) {
			e.Error(apis.ResetPullStreamError.Code, err, fmt.Sprintf("当前存在开启的拉流直播，请关闭后重试"))
		} else if err.Error() == string(apis.LiveStartError.Code) {
			e.Error(apis.LiveStartError.Code, err, fmt.Sprintf("当前存在开启的拉流直播，请关闭后重试"))
		} else if err.Error() == string(apis.MaxLiveDurationError.Code) {
			e.Error(apis.MaxLiveDurationError.Code, err, fmt.Sprintf("直播时间超过最大直播时长"))
		} else {
			e.Error(apis.StartLiveError.Code, err, fmt.Sprintf("开启直播失败，失败信息 %s", err.Error()))
		}
		return
	}

	e.OK("", "开启直播成功")
}

// EndLive 关闭直播
func (e LiveBasic) EndLive(c *gin.Context) {
	req := dto.StartOrEndLiveReq{}
	s := service.LiveBasic{}
	r := service.LiveRecord{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		MakeService(&r.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.EndLive(&req)
	if err != nil {
		if err.Error() == string(apis.LiveEndError.Code) {
			e.Error(apis.LiveEndError.Code, err, fmt.Sprintf("关闭直播失败，%s", "直播间已经停止"))
		} else {
			e.Error(apis.EndLiveError.Code, err, fmt.Sprintf("关闭直播失败，失败信息 %s", err.Error()))
		}

		return
	}

	//如果是结束直播 需要关闭录制
	if req.LiveType == 4 {
		r.StopLiveRecord(&dto.StopRecordReq{TenantId: req.TenantId, BasicId: req.BasicId})
	}

	e.OK("", "关闭直播成功")
}

func (e LiveBasic) StopLiveTask(c *gin.Context) {
	req := dto.StopLiveTaskReq{}
	s := service.LiveBasic{}
	r := service.LiveRecord{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		MakeService(&r.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.StopLiveTask(&req)
	if err != nil {
		e.Error(apis.EndLiveError.Code, err, fmt.Sprintf("关闭直播失败，失败信息 %s", err.Error()))
		return
	}

	e.OK("", "关闭直播成功")
}

// GetToken 获取token
func (e LiveBasic) GetToken(c *gin.Context) {
	req := dto.GetTokenReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetToken(&req)
	if err != nil {
		e.Error(apis.EndLiveError.Code, err, fmt.Sprintf("获取token失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取token成功")
}

func (e LiveBasic) UpdateBasicInfo(c *gin.Context) {
	req := dto.UpdateBasicInfoReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	isExisted := s.IsExitLiveBasic(&dto.LiveBasicReq{Id: req.Id, LiveName: req.LiveName, TenantId: req.TenantId})
	if isExisted {
		e.Custom("", apis.RepeatedDataError)
		return
	}

	err = s.UpdateBasicInfo(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改LiveBasic失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(req.Id, "修改成功")
}

func (e LiveBasic) SaveGuidePage(c *gin.Context) {
	req := dto.UpdateGuidePageReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	isExisted := s.IsExitLiveBasic(&dto.LiveBasicReq{Id: req.LiveId, LiveName: req.LiveName, TenantId: req.TenantId})
	if isExisted {
		e.Custom("", apis.RepeatedDataError)
		return
	}

	err = s.SaveGuidePage(&req)
	if err != nil {
		if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.ImageError.Code, err, "修改引导页失败: 图片地址不正确")
		} else {
			e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改引导页失败，失败信息 %s", err.Error()))
		}

		return
	}

	e.OK(req.Id, "修改引导页成功")
}

func (e LiveBasic) SaveVirtualNum(c *gin.Context) {
	req := dto.UpdateVirtualNumReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SaveVirtualNum(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改虚拟人数失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(req.Id, "修改虚拟人数成功")
}

func (e LiveBasic) EnableOrStopIM(c *gin.Context) {
	req := dto.UpdateIMReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	if req.IsEnable == 1 && len(req.CreateIM.GroupId) <= 0 {
		e.Error(apis.ParamterValidatorError.Code, err, "启用弹幕时 弹幕参数不为空")
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.EnableOrStopIMError.Code, err, fmt.Sprintf("修改弹幕设置失败，失败信息 %s", err.Error()))
		return
	}
	err = s.EnableOrStopIM(tx, &req)
	if err != nil {
		tx.Rollback()
		e.Error(apis.EnableOrStopIMError.Code, err, fmt.Sprintf("修改弹幕设置失败，失败信息 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(req.Id, "修改弹幕设置成功")
}

func (e LiveBasic) EnableOrStopLikes(c *gin.Context) {
	req := dto.UpdateLikesReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改点赞设置失败，失败信息 %s", err.Error()))
		return
	}
	err = s.EnableOrStopLikes(tx, &req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改点赞设置失败，失败信息 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(req.Id, "修改点赞设置成功")
}

func (e LiveBasic) EnableOrStopComment(c *gin.Context) {
	req := dto.UpdateCommentReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改评论设置失败，失败信息 %s", err.Error()))
		return
	}
	err = s.EnableOrStopComment(tx, &req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改评论设置失败，失败信息 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(req.Id, "修改评论设置成功")
}

func (e LiveBasic) EnableOrStopCountDown(c *gin.Context) {
	req := dto.UpdateCountDownReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改倒计时设置失败，失败信息 %s", err.Error()))
		return
	}

	err = s.EnableOrStopCountDown(tx, &req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改倒计时设置失败，失败信息 %s", err.Error()))
		return
	}

	tx.Commit()
	e.OK(req.Id, "修改倒计时设置成功")
}

func (e LiveBasic) EnableOrStopReservation(c *gin.Context) {
	req := dto.UpdateReservationReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.EnableOrStopReservation(&req)
	if err != nil {
		e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("修改预约观看设置失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(req.Id, "修改预约观看设置成功")
}

// SavePullStreamAddress 保存拉流地址
func (e LiveBasic) SavePullStreamAddress(c *gin.Context) {
	req := dto.PullStreamLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	err = s.SavePullStreamAddress(&req)
	if err != nil {
		e.Error(apis.SavePullStreamError.Code, err, fmt.Sprintf("保存拉流地址失败，失败信息 %s", err.Error()))
		return
	}

	e.OK("", "保存拉流地址成功")
}

// StartPullStreamLive 开启拉流直播
func (e LiveBasic) StartPullStreamLive(c *gin.Context) {
	req := dto.StartPullStreamLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.StartPullStreamLiveError.Code, err, fmt.Sprintf("开启拉流直播失败，失败信息 %s", err.Error()))
		return
	}
	err = s.StartPullStreamLive(tx, &req)
	if err != nil {
		e.Error(apis.StartPullStreamLiveError.Code, err, fmt.Sprintf("开启拉流直播失败，失败信息 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK("", "开启拉流直播成功")
}

func (e LiveBasic) StopPullStreamLive(c *gin.Context) {
	req := dto.StopPullStreamLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.StartPullStreamLiveError.Code, err, fmt.Sprintf("开启拉流直播失败，失败信息 %s", err.Error()))
		return
	}
	err = s.StopPullStreamLive(tx, &req)
	if err != nil {
		e.Error(apis.StopPullStreamLiveError.Code, err, fmt.Sprintf("停止拉流直播失败，失败信息 %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK("", "开启拉流直播成功")
}

func (e LiveBasic) GetPersonalization(c *gin.Context) {
	req := dto.GetPersonalReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPersonalization(&req)
	if err != nil {
		e.Error(apis.GetPersonalizationError.Code, err, fmt.Sprintf("获取个性化设置失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取个性化设置成功")
}

func (e LiveBasic) GetForLive(c *gin.Context) {
	req := dto.LiveBasicGetReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	var model live.LiveBasicModelForLive
	err = s.GetForLive(&req, &model)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取直播间信息失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(model, "获取直播间信息成功")
}

func (e LiveBasic) GetLiveState(c *gin.Context) {
	req := dto.GetLiveStateReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetLiveState(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取直播间状态失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取直播间状态成功")
}

func (e LiveBasic) GetWechatAccessToken(c *gin.Context) {
	req := wcDto.WechatReq{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetAccessToken(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信token失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取微信token成功")
}

func (e LiveBasic) WechatCallBack(c *gin.Context) {

	req := wcDto.GetWechatCallBack{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	req.Method = c.Request.Method
	req.MsgSignature = c.Query("msg_signature")
	req.Timestamp = c.Query("timestamp")
	req.Nonce = c.Query("nonce")
	req.Echostr = c.Query("echostr")
	xmlSliceData, _ := c.GetRawData()
	fmt.Println(string(xmlSliceData))

	if len(xmlSliceData) > 0 {
		if err = xml.Unmarshal(xmlSliceData, &req); err != nil {
			e.Error(apis.InternalError.Code, err, "xml解析错误")
			return
		}
	}

	if len(req.Encrypt) > 0 {
		req.Echostr = req.Encrypt
	}

	data, err := s.GetDataCallBack(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信token失败，失败信息 %s", err.Error()))
		return
	}

	if req.Method == "GET" {
		c.JSON(200, data)
	} else {
		c.Status(200)
		c.Writer.WriteString("success")
	}
}

func (e LiveBasic) WechatCallBackForScan(c *gin.Context) {

	req := wcDto.GetWechatCallBack{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	req.Method = c.Request.Method
	req.MsgSignature = c.Query("msg_signature")
	req.Timestamp = c.Query("timestamp")
	req.Nonce = c.Query("nonce")
	req.Echostr = c.Query("echostr")
	xmlSliceData, _ := c.GetRawData()

	if len(xmlSliceData) > 0 {
		if err = xml.Unmarshal(xmlSliceData, &req); err != nil {
			e.Error(apis.InternalError.Code, err, "xml解析错误")
			return
		}
	}

	if len(req.Encrypt) > 0 {
		req.Echostr = req.Encrypt
	}

	data, err := s.GetDataCallBackForScan(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信token失败，失败信息 %s", err.Error()))
		return
	}

	if req.Method == "GET" {
		c.JSON(200, data)
	} else {
		c.Status(200)
		c.Writer.WriteString("success")
	}
}

func (e LiveBasic) GetWechatAccessSuiteToken(c *gin.Context) {
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetSuiteAccessToken()
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信token失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取微信token成功")
}

func (e LiveBasic) GetSuiteAccessTokenForScan(c *gin.Context) {
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetSuiteAccessTokenForScan()
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信token失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取微信token成功")
}

func (e LiveBasic) GetProviderToken(c *gin.Context) {
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetProviderToken()
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信服务商的token失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取微信服务商的token成功")
}

func (e LiveBasic) GetPreCode(c *gin.Context) {
	req := wcDto.GetPreCode{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetPreCode(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信预授权码失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取微信服预授权码成功")
}

func (e LiveBasic) GetPermanenCode(c *gin.Context) {
	req := wcDto.GetPermanentCode{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetPermanentCode(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信预授权码失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取微信服预授权码成功")
}

func (e LiveBasic) GetCorpToken(c *gin.Context) {
	req := wcDto.GetCorpToken{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetCorpToken(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取企业凭证失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取企业凭证成功")
}

func (e LiveBasic) GetCorpTokenCache(c *gin.Context) {
	req := wcDto.GetCorpToken{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetCorpTokenCache(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取企业凭证失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取企业凭证成功")
}

func (e LiveBasic) GetApiTicket(c *gin.Context) {
	req := wcDto.GetApiTicket{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetApiTicket(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取api_ticket失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取api_ticket成功")
}

func (e LiveBasic) GetApiTicketWithoutType(c *gin.Context) {
	req := wcDto.GetApiTicket{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	token, err := s.GetApiTicketWithoutType(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取api_ticket失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(token, "获取api_ticket成功")
}

func (e LiveBasic) GetSignature(c *gin.Context) {
	req := wcDto.GetSignature{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data := s.GetSignature(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取签名失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取签名成功")
}

func (e LiveBasic) GetLoginInfo(c *gin.Context) {
	req := wcDto.GetLoginInfo{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetLoginInfo(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取用户登录信息失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取用户登录信息成功")
}

func (e LiveBasic) GetWeChatInfo(c *gin.Context) {
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetWeChatInfo()
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取微信基本信息失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取微信基本信息成功")
}

func (e LiveBasic) SetAuthCode(c *gin.Context) {
	req := wcDto.SetAuthCode{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SetAuthCode(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取预授权失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(nil, "获取预授权成功")
}

func (e LiveBasic) GetUserInfo(c *gin.Context) {
	req := wcDto.GetUserInfo{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetUserInfo(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取访问用户身份信息失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取访问用户身份信息成功")
}

func (e LiveBasic) GetSpecialUserInfo(c *gin.Context) {
	req := wcDto.GetSpecialUserInfo{}
	s := wc.WechatService{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetSpecialUserInfo(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取访问敏感用户身份信息失败，失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "获取访问敏感用户身份信息成功")
}

func (e LiveBasic) SavePortal(c *gin.Context) {
	req := dto.SavePortal{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	err = s.SavePortal(&req)
	if err != nil {
		if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.ImageError.Code, err, "保存门户信息失败: 图片地址不正确")
		} else {
			e.Error(apis.DBUpdateError.Code, err, fmt.Sprintf("保存门户信息失败， %s", err.Error()))
		}

		return
	}

	e.OK(nil, "保存门户信息成功")
}

func (e LiveBasic) GetPortal(c *gin.Context) {
	req := dto.GetPortal{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPortal(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取门户信息失败， %s", err.Error()))
		return
	}

	e.OK(data, "获取门户信息成功")
}

func (e LiveBasic) GetPortalInfo(c *gin.Context) {
	req := dto.GetPortalInfo{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPortalInfo(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取门户信息失败， %s", err.Error()))
		return
	}

	e.OK(data, "获取门户信息成功")
}

func (e LiveBasic) ConfirmRecord(c *gin.Context) {
	req := dto.ConfirmRecord{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	tx := e.Orm.Begin()
	defer func() {
		if r := recover(); r != nil {
			tx.Rollback()
		}
	}()
	if err = tx.Error; err != nil {
		e.Error(apis.InternalError.Code, err, err.Error())
	}
	err = s.ConfirmRecord(tx, &req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("确认录制文件同步信息失败， %s", err.Error()))
		return
	}
	tx.Commit()
	e.OK(nil, "确认录制文件同步信息成功")
}

func (e LiveBasic) SavePublishSetting(c *gin.Context) {
	req := dto.SavePublishSetting{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SavePublishSetting(&req)
	if err != nil {
		if err.Error() == string(apis.ImageError.Code) {
			e.Error(apis.ImageError.Code, err, "保存发布信息设置失败: 图片地址不正确")
		} else {
			e.Error(apis.SavePublishError.Code, err, fmt.Sprintf("保存发布信息设置失败， %s", err.Error()))
		}

		return
	}
	e.OK(req.Id, "保存发布信息设置成功")
}

func (e LiveBasic) GetPublishSetting(c *gin.Context) {
	req := dto.GetPublishSetting{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPublishSetting(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取发布信息设置失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取发布信息设置成功")
}

func (e LiveBasic) MoveView(c *gin.Context) {
	req := dto.MoveViewReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.MoveView(&req)
	if err != nil {
		e.Error(apis.MoveViewError.Code, err, fmt.Sprintf("移动视角失败， %s", err.Error()))
		return
	}
	e.OK(nil, "移动视角成功")
}

func (e LiveBasic) GetPlatformList(c *gin.Context) {
	req := dto.GetPlatformListReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetPlatformList(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取平台失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取平台成功")
}

func (e LiveBasic) CreatePlatform(c *gin.Context) {
	req := dto.CreatePlatformReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	req.TenantId = c.Query("permissionTenantId")
	err = s.CreatePlatform(&req)
	if err != nil {
		e.Error(apis.CreatePlatformError.Code, err, fmt.Sprintf("创建平台失败， %s", err.Error()))
		return
	}
	e.OK(req.Id, "创建平台成功")
}

func (e LiveBasic) UpdatePlatform(c *gin.Context) {
	req := dto.UpdatePlatformReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	err = s.UpdatePlatform(&req)
	if err != nil {
		e.Error(apis.UpdatePlatformError.Code, err, fmt.Sprintf("更新平台失败， %s", err.Error()))
		return
	}
	e.OK(req.Id, "更新平台成功")
}

func (e LiveBasic) GetPlatform(c *gin.Context) {
	req := dto.GetPlatformReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	if len(req.TenantId) <= 0 {
		req.TenantId = c.Query("permissionTenantId")
	}
	data, err := s.GetPlatform(&req)
	if err != nil {
		e.Error(apis.UpdatePlatformError.Code, err, fmt.Sprintf("获取平台信息失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取平台信息成功")
}

func (e LiveBasic) DeletePlatform(c *gin.Context) {
	req := dto.DeletePlatformReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeletePlatform(&req)
	if err != nil {
		e.Error(apis.DeletePlatformError.Code, err, fmt.Sprintf("删除平台失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除平台成功")
}

func (e LiveBasic) StartPlatform(c *gin.Context) {
	req := dto.StartPlatformReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StartPlatform(&req)
	if err != nil {
		e.Error(apis.StartPlatformError.Code, err, fmt.Sprintf("开启平台失败， %s", err.Error()))
		return
	}
	e.OK(nil, "开启平台成功")
}

func (e LiveBasic) StopPlatform(c *gin.Context) {
	req := dto.StopPlatformReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopPlatform(&req)
	if err != nil {
		e.Error(apis.StopPlatformError.Code, err, fmt.Sprintf("停止平台转推失败， %s", err.Error()))
		return
	}
	e.OK(nil, "停止平台转推成功")
}

func (e LiveBasic) SetBlackUser(c *gin.Context) {
	req := dto.SetBlackUserReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.SetBlackUser(&req)
	if err != nil {
		e.Error(apis.StopPlatformError.Code, err, fmt.Sprintf("设置黑名单失败， %s", err.Error()))
		return
	}
	e.OK(nil, "设置黑名单成功")
}

func (e LiveBasic) DeleteBlackUser(c *gin.Context) {
	req := dto.DeleteBlackUserReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.DeleteBlackUser(&req)
	if err != nil {
		e.Error(apis.StopPlatformError.Code, err, fmt.Sprintf("删除黑名单失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除黑名单成功")
}

func (e LiveBasic) GetBlackUser(c *gin.Context) {
	req := dto.GetBlackUserReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetBlackUser(&req)
	if err != nil {
		e.Error(apis.StopPlatformError.Code, err, fmt.Sprintf("获取黑名单失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取黑名单成功")
}

func (e LiveBasic) StartPCLive(c *gin.Context) {
	req := dto.StartPCLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.StartPCLive(&req)
	if err != nil {
		e.Error(apis.StartPCLiveError.Code, err, fmt.Sprintf("开启pc直播失败失败， %s", err.Error()))
		return
	}
	e.OK(data, "开启pc直播成功")
}

func (e LiveBasic) StopPCLive(c *gin.Context) {
	req := dto.StopPCLiveReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.StopPCLive(&req)
	if err != nil {
		e.Error(apis.StopPCLiveError.Code, err, fmt.Sprintf("停止pc直播失败失败， %s", err.Error()))
		return
	}
	e.OK(nil, "停止pc直播成功")
}

func (e LiveBasic) GetWsList(c *gin.Context) {
	req := vrmDto.WsListReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetWsList(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取文件列表失败， %s", err.Error()))
		return
	}
	e.OK(data.Data, "获取文件列表失败成功")
}

func (e LiveBasic) GetNeedWsList(c *gin.Context) {
	req := vrmDto.WsListReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetNeedWsList(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取文件列表失败， %s", err.Error()))
		return
	}
	e.OK(data.Data, "获取文件列表失败成功")
}

func (e LiveBasic) GetLiveDuration(c *gin.Context) {
	req := dto.GetLiveDurationReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetLiveDuration(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取直播时长失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取直播时长成功")
}

func (e LiveBasic) GetCon(c *gin.Context) {
	req := dto.GetConNumReq{}
	s := service.LiveBasic{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetConNum(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取连接数失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取连接数成功")
}
